using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmartContentUI.Document
{
    public partial class AddDocumentForm : UserControl
    {
        #region Variables
        private SmartContentBusiness.Session session;
        private SmartContentDB.Interfaces.Field[] columns;
        private string table;
        private int id;

        public delegate void DocumentAdded();
        public event DocumentAdded AddedDoc;
        #endregion

        #region Constructor
        public AddDocumentForm(SmartContentBusiness.Session ses, string table)
        {
            InitializeComponent();
            this.session = ses;
            this.table = table;
            this.id = -1;
        }
        #endregion

        #region Functions
        public void createForm(SmartContentDB.Interfaces.Field[] fields)
        {
            columns = fields;
            Label field;
            Control val;
            Label info;

            int field_x = 16;
            int field_y = 16;
            int val_x = 152;
            int val_y = 13;
            int info_x = 279;
            int info_y = 16;

            ImageList imglst = new ImageList();
            imglst.ColorDepth = ColorDepth.Depth32Bit;
            imglst.Images.Add(global::SmartContentUI.Properties.Resources._true);
            imglst.Images.Add(global::SmartContentUI.Properties.Resources._false);

            for (int i = 0; i < fields.Length ; i++)
            {
                field = new Label();
                field.Text = fields[i].getName();
                field.TextAlign = ContentAlignment.TopRight;
                field.Location = new Point(field_x, field_y);
                field.Size = new Size(130, 17);
                if (fields[i].getType().ToUpper().Equals("TINYINT"))
                {
                    val = new Util.EXComboBox();
                    val.Location = new Point(val_x, val_y);
                    val.Size = new Size(121,20);
                    ((Util.EXComboBox)val).DropDownStyle = ComboBoxStyle.DropDownList;
                    ((Util.EXComboBox)val).Items.Add(new Util.EXComboBox.EXImageItem(imglst.Images[0], "1"));
                    ((Util.EXComboBox)val).Items.Add(new Util.EXComboBox.EXImageItem(imglst.Images[1], "0"));
                    ((Util.EXComboBox)val).SelectedIndex = 1;
                }
                else if (fields[i].getType().ToUpper().Equals("DATETIME"))
                {
                    val = new DateTimePicker();
                    ((DateTimePicker)val).Format = DateTimePickerFormat.Custom;
                    ((DateTimePicker)val).CustomFormat = "dd/MM/yyyy";
                    val.Location = new Point(val_x, val_y);
                    val.Size = new Size(121, 20);
                }
                else
                {
                    val = new TextBox();
                    val.Location = new Point(val_x, val_y);
                    val.Size = new Size(121, 20);
                }
                info = new Label();
                info.TextAlign = ContentAlignment.TopLeft;
                info.ForeColor = Color.Red;
                info.Location = new Point(info_x, info_y);
                info.Size = new Size(130, 17);
                if (!fields[i].IsNullable())
                {
                    info.Text = "*";
                }
                this.panel1.Controls.Add(field);
                this.panel1.Controls.Add(val);
                this.panel1.Controls.Add(info);
                field_y += 26;
                info_y += 26;
                val_y += 26;
            }
        }

        public void clearForm()
        {
            this.id = -1;
            for (int i = 0; i < this.panel1.Controls.Count; i++)
            {
                if (isInfoControl(this.panel1.Controls[i]))
                {
                    if (this.panel1.Controls[i].GetType() == typeof(Util.EXComboBox))
                        ((Util.EXComboBox)this.panel1.Controls[i]).SelectedIndex = 1;
                    else
                        this.panel1.Controls[i].Text = "";
                }
            }
        }

        public string[] getDocumentForm()
        {
            System.Collections.ArrayList value_list = new System.Collections.ArrayList();
            for (int i = 0; i < this.panel1.Controls.Count; i++)
            {
                if(isInfoControl(this.panel1.Controls[i]))
                {
                    if (this.panel1.Controls[i].GetType() == typeof(Util.EXComboBox))
                    {
                        value_list.Add(((Util.EXComboBox.EXItem)((Util.EXComboBox)this.panel1.Controls[i]).SelectedItem).MyValue);
                    }
                    else if (this.panel1.Controls[i].GetType() == typeof(DateTimePicker))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("{0:yyyy-MM-dd}", ((DateTimePicker)this.panel1.Controls[i]).Value);
                        value_list.Add(sb.ToString());
                    }
                    else
                    {
                        value_list.Add(this.panel1.Controls[i].Text);
                    }
                }
            }
            string[] values = new string[value_list.Count];
            value_list.CopyTo(values);
            return values;
        }

        private bool isInfoControl(Control ctrl)
        {
            if (ctrl.GetType() == typeof(Util.EXComboBox))
                return true;
            else if (ctrl.GetType() == typeof(TextBox))
                return true;
            else if (ctrl.GetType() == typeof(DateTimePicker))
                return true;
            else
                return false;
        }

        public void fillForm(SmartContentDB.Interfaces.Field[] fields, string[] values, int id)
        {
            this.id = id;
            for (int cp = 1, cf = 0; cp < this.panel1.Controls.Count && cf < fields.Length; cp += 3, cf++ )
            {
                if (fields[cf].getType().ToUpper().Equals("TINYINT"))
                {
                    ((Util.EXComboBox)this.panel1.Controls[cp]).SelectedIndex = values[cf].Equals("1") ? 0 : 1;
                }
                else if (fields[cf].getType().ToUpper().Equals("DATETIME"))
                {
                    ((DateTimePicker)this.panel1.Controls[cp]).Text = values[cf];
                }
                else
                {
                    ((TextBox)this.panel1.Controls[cp]).Text = values[cf];
                }
            }
        }

        public int getId()
        {
            return this.id;
        }
        #endregion

    }
}
